namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements.PatternServants;
    using Guia.Locator;

    /// <summary>
    /// This class represents a TabItem. A <see cref="TabControl"/> has multiple TabItems.
    /// </summary>
    public class TabItem : UiElement, ISelectionItemSimple
    {
        /// <summary>
        /// Represents a helper-servant for the SelectionItem-pattern.
        /// </summary>
        private readonly ISelectionItemSimple selectionItemServant;

        /// <summary>
        /// Initializes a new instance of the <see cref="TabItem"/> class.
        /// </summary>
        /// <param name="automationElement">
        /// The automation element.
        /// </param>
        public TabItem(AutomationElement automationElement) : base(automationElement)
        {
            this.selectionItemServant = new SelectionItemImpl(this);
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.TabItem); }
        }

        /// <summary>
        /// Gets the Header of the TabItem.
        /// </summary>
        public string Header
        {
            get
            {
                return this.automationElement.Current.Name;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the TabItem is selected or not.
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return this.selectionItemServant.IsSelected;
            }
        }

        /// <summary>
        /// Selects the TabItem.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the TabItem is disabled.</exception>
        public void Select()
        {
            this.selectionItemServant.Select();
        }
    }
}